﻿class Loader {
	//------------------------------------属性
	[Inspectable(defaultValue="")]
	public var loadingFullMC:String;
	[Inspectable(defaultValue="")]
	public var loadingPr:String;
	[Inspectable(defaultValue="")]
	public var loadingTextF:String;
	[Inspectable(defaultValue="")]
	public var loadingIcon:String;
	[Inspectable(defaultValue="")]
	public var loadingMsgF:String;
	[Inspectable(defaultValue="")]
	public var loadingRetry:String;
	[Inspectable(defaultValue="")]
	public var loadingCencel:String;
	//-------------------------------------变量
	private var tag:Object;	//目标
	private var url:String; //加载地址
	private var data:Object;	//数据存取缓存
	private var funObj:Object;	//完成加载后的目标
	private var funData:Object;	//完成加载后目标参数
	//----
	private var me:Object;//自己
	//---
	private var pageid:Number;    //加载动态页面的进程id
	private var picid:Number;     //加载图片的进程Id
	private var flashid:Number;   //加载swf文件的进程id
	private var timeoutid:Number; //超时时间id
	//-------------------------------------构造函数
	public function Loader() {
		//---------- 初始化
		init();
	}
	private function init() {
		tag = new Object();
		tag = null;
		funObj=undefined;
		funData=undefined;
		me=this;
		url=null;
	}
	private function say(o:Object) {
		trace("loading: "+o.toString());
	}
	//------------------------------------功能函数
	public function load(lx:String, urls:String, tag1:Object, tag2:Object, data:Object) {
		if ((tag == undefined || tag == null) && (tag1 == undefined || tag1 == null)) {
			say("加载目标未定义！");
			return;
		} else {
			tag = tag1;
		}
		if(urls==undefined || urls==null || urls=="")
		{
			say("加载地址为空！");
			if(url!=undefined && url!=null && url!="")
			{
				say("沿用之前的地址。")
			}
			return ;
		}else{
			url=urls;
		}
		if (tag2 == undefined || tag2 == null) {
			funObj = undefined;
			funData = undefined;
		} else {
			funObj = tag2;
		}
		if(data ==undefined || data==null)
		{
			funData=undefined;
		}else{
			funData=new Object();
			funData=data;
		}
		//--------- 加载
		loadbg(lx);
	}
//============================================================= 内部功能函数
	private function loadbg(lx:String){
		if(lx.indexOf("flash",0)!=-1)
		{
			//加载swf
			
		}
		if(lx.indexOf("pic",0)!=-1)
		{
			
			if(url.split(".")[(url.split(".").length-1)]!="jpg")
			{
				say("加载图片类型不正确[."+url.split(".")[(url.split(".").length-1)]+"]");
			}else{
				//加载图片
				
			}
		}
		if(lx.indexOf("page",0)!=-1)
		{
			//加载 动态页面数据
			data=new LoadVars();
			data.load(url);
			pageid=setInterval(loadingpage,1,me);
		}
	};
	private function loadingpage(me:Object){
		var bfb:Number=(me.getBytesLoaded()/me.getBytesTotal());
		bfb*=10000;
		bfb=int(bfb);
		bfb/=100;
		if(bfb<100)
		{
			
		}else{
			endloading(me);
		}
	};
	private function endloading(me:Object)
	{
		clearInterval(me.pageid);
		clearInterval(me.picid);
		clearInterval(me.flashid);
		clearInterval(me.timeoutid);
		if(me.funObj!=undefined && me.funObj!=null)
		{
			if(typeof(me.funObj)=="movieclip"){
				me.say("播放mc。数据在mc.loading_data中。");
				me.tag.loading_data=new Object();
				me.tag.loading_data=me.funData;
				me.tag.play();
			}
			if(typeof(me.funObj)=="function"){
				me.say("调用函数。参数["+me.funData+"]");
				var f:Function;
				f=me.funObj;
				f(me.data,me.funData)
			}
		}
	}
}
